Skip to content

Commit

Permalink
Add optional tmpdirBase attribute
Browse files Browse the repository at this point in the history
Facebook createreactapp ModuleScopePlugin enforced all files to be
children of the source directory.

A simple `tmpdirBase: paths.appSrc,` addition to the plugin
configuration will ensure conformance to this rule.

Signed-off-by: Frédéric Miserey <[email protected]>

Closes #60
  • Loading branch information
Frédéric Miserey authored and rxaviers committed Jun 16, 2017
1 parent 0b3faf9 commit 1e508fe
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
3 changes: 2 additions & 1 deletion DevelopmentModePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class DevelopmentModePlugin {
constructor(attributes) {
let i18nDataTemplate, messages;
const cldr = attributes.cldr || util.cldr;
const tmpdir = util.tmpdir();
const tmpdirBase = attributes.tmpdirBase || ".";
const tmpdir = util.tmpdir(tmpdirBase);

messages = attributes.messages && util.readMessages(attributes.messages, attributes.developmentLocale);

Expand Down
3 changes: 2 additions & 1 deletion ProductionModePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class ProductionModePlugin {
this.moduleFilter = util.moduleFilterFn(attributes.moduleFilter);
this.supportedLocales = attributes.supportedLocales;
this.output = attributes.output;
this.tmpdir = util.tmpdir();
const tmpdirBase = attributes.tmpdirBase || ".";
this.tmpdir = util.tmpdir(tmpdirBase);
}

apply(compiler) {
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ new globalizePlugin({
supportedLocales: [ "en", "es", "zh", ... ], // locales that should be built support for.
messages: "messages/[locale].json", // messages (optional)
output: "globalize-compiled-data-[locale].[hash].js", // build output.
moduleFilter: filterFunction // filter for modules to exclude from processing
moduleFilter: filterFunction, // filter for modules to exclude from processing
tempdirBase: ".", // optional for non create-react-apps
})
```

Expand All @@ -54,6 +55,8 @@ new globalizePlugin({

*moduleFilter* is a function to test on filepaths, and optionally reject matching files from further processing. See [react-globalize-webpack-plugin](https://github.com/rxaviers/react-globalize-webpack-plugin) for an example usage. Globalize's internal modules are not processed by default.

*tmpdirBase* tells the plugin where to create its temporary files ; it should be set it to `paths.appSrc` in ejected [create-react-app](https://github.com/facebookincubator/create-react-app)s to comply with its ModuleScopePlugin.

## Example

See https://github.com/jquery/globalize/tree/master/examples/app-npm-webpack.
Expand Down
26 changes: 19 additions & 7 deletions test/ProductionModePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ const rimraf = require("rimraf");
const webpack = require("webpack");

const TEST_CASES = {
/*
tempdir should be first so that the top-level temp directory
created for the latter cases doesn't interfere.
*/
tempdir: [],
default: [],
named: [ new webpack.NamedModulesPlugin() ],
hashed: [ new webpack.HashedModuleIdsPlugin() ]
};

const outputPath = (key, file) => path.join(__dirname, "../_test-output", key, file || "");

const tmpdirBase = (key) => key === "tempdir" ? {tmpdirBase: outputPath(key)} : {};

const mkWebpackConfig = (key) => ({
entry: {
app: path.join(__dirname, "fixtures/app")
Expand All @@ -25,13 +32,18 @@ const mkWebpackConfig = (key) => ({
filename: "app.js"
},
plugins: TEST_CASES[key].concat([
new GlobalizePlugin({
production: true,
developmentLocale: "en",
supportedLocales: ["en", "es"],
messages: path.join(__dirname, "fixtures/translations/[locale].json"),
output: "[locale].js"
}),
new GlobalizePlugin(
Object.assign(
{
production: true,
developmentLocale: "en",
supportedLocales: ["en", "es"],
messages: path.join(__dirname, "fixtures/translations/[locale].json"),
output: "[locale].js"
},
tmpdirBase(key)
)
),
new webpack.optimize.CommonsChunkPlugin({
name: "vendor",
filename: "vendor.js",
Expand Down
4 changes: 2 additions & 2 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ module.exports = {
return JSON.parse(fs.readFileSync(messagesFilepath));
},

tmpdir: () => {
const tmpdir = path.resolve("./.tmp-globalize-webpack");
tmpdir: (base) => {
const tmpdir = path.resolve(path.join(base, ".tmp-globalize-webpack"));
if (!fs.existsSync(tmpdir)) {
fs.mkdirSync(tmpdir);
} else {
Expand Down

0 comments on commit 1e508fe

Please sign in to comment.