-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- use let/const/arrows - add no-var lint rule - 'use strict' everywhere - drop webpack 1 support Ref #55 Closes #58 Signed-off-by: James Bellenger <jbellenger@twitter.com>
- Loading branch information
Showing
10 changed files
with
533 additions
and
550 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,66 @@ | ||
var CommonJsRequireDependency = require("webpack/lib/dependencies/CommonJsRequireDependency"); | ||
var fs = require("fs"); | ||
var NormalModuleFactory = require("webpack/lib/NormalModuleFactory"); | ||
var path = require("path"); | ||
var SkipAMDPlugin = require("skip-amd-webpack-plugin"); | ||
var util = require("./util"); | ||
"use strict"; | ||
|
||
const CommonJsRequireDependency = require("webpack/lib/dependencies/CommonJsRequireDependency"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const SkipAMDPlugin = require("skip-amd-webpack-plugin"); | ||
const util = require("./util"); | ||
|
||
/** | ||
* Development Mode: | ||
* - Automatically loads CLDR data (i.e., injects `Globalize.load(<necessary CLDR data>)`). | ||
* - Automatically define default locale (i.e., injects `Globalize.locale(<defaultLocale>)`). | ||
*/ | ||
function DevelopmentModePlugin(attributes) { | ||
var i18nDataTemplate, messages; | ||
var cldr = attributes.cldr || util.cldr; | ||
var tmpdir = util.tmpdir(); | ||
|
||
messages = attributes.messages && util.readMessages(attributes.messages, attributes.developmentLocale); | ||
class DevelopmentModePlugin { | ||
constructor(attributes) { | ||
let i18nDataTemplate, messages; | ||
const cldr = attributes.cldr || util.cldr; | ||
const tmpdir = util.tmpdir(); | ||
|
||
i18nDataTemplate = [ | ||
"var Globalize = require(\"globalize\");", | ||
"", | ||
"Globalize.load(" + JSON.stringify(cldr(attributes.developmentLocale)) + ");", | ||
messages ? "Globalize.loadMessages(" + JSON.stringify(messages) + ");": "", | ||
"Globalize.locale(" + JSON.stringify(attributes.developmentLocale) + ");", | ||
"", | ||
"module.exports = Globalize;" | ||
].join("\n"); | ||
|
||
this.i18nData = path.join(tmpdir, "dev-i18n-data.js"); | ||
this.moduleFilter = util.moduleFilterFn(attributes.moduleFilter); | ||
fs.writeFileSync(this.i18nData, i18nDataTemplate); | ||
} | ||
messages = attributes.messages && util.readMessages(attributes.messages, attributes.developmentLocale); | ||
|
||
DevelopmentModePlugin.prototype.apply = function(compiler) { | ||
var i18nData = this.i18nData; | ||
var moduleFilter = this.moduleFilter; | ||
i18nDataTemplate = [ | ||
"var Globalize = require(\"globalize\");", | ||
"", | ||
`Globalize.load(${JSON.stringify(cldr(attributes.developmentLocale))});`, | ||
messages ? `Globalize.loadMessages(${JSON.stringify(messages)});` : "", | ||
`Globalize.locale(${JSON.stringify(attributes.developmentLocale)});`, | ||
"", | ||
"module.exports = Globalize;" | ||
].join("\n"); | ||
|
||
// Skip AMD part of Globalize Runtime UMD wrapper. | ||
compiler.apply(new SkipAMDPlugin(/(^|[\/\\])globalize($|[\/\\])/)); | ||
this.i18nData = path.join(tmpdir, "dev-i18n-data.js"); | ||
this.moduleFilter = util.moduleFilterFn(attributes.moduleFilter); | ||
fs.writeFileSync(this.i18nData, i18nDataTemplate); | ||
} | ||
|
||
// "Intercepts" all `require("globalize")` by transforming them into a | ||
// `require` to our custom generated template, which in turn requires | ||
// Globalize, loads CLDR, set the default locale and then exports the | ||
// Globalize object. | ||
var bindParser = function(parser) { | ||
parser.plugin("call require:commonjs:item", function(expr, param) { | ||
var request = this.state.current.request; | ||
apply(compiler) { | ||
// Skip AMD part of Globalize Runtime UMD wrapper. | ||
compiler.apply(new SkipAMDPlugin(/(^|[\/\\])globalize($|[\/\\])/)); | ||
|
||
if(param.isString() && param.string === "globalize" && moduleFilter(request) && | ||
!(new RegExp(util.escapeRegex(i18nData))).test(request)) { | ||
var dep; | ||
// "Intercepts" all `require("globalize")` by transforming them into a | ||
// `require` to our custom generated template, which in turn requires | ||
// Globalize, loads CLDR, set the default locale and then exports the | ||
// Globalize object. | ||
compiler.plugin("compilation", (compilation, params) => { | ||
params.normalModuleFactory.plugin("parser", (parser) => { | ||
parser.plugin("call require:commonjs:item", (expr, param) => { | ||
const request = parser.state.current.request; | ||
|
||
dep = new CommonJsRequireDependency(i18nData, param.range); | ||
dep.loc = expr.loc; | ||
dep.optional = !!this.scope.inTry; | ||
this.state.current.addDependency(dep); | ||
if(param.isString() && param.string === "globalize" && this.moduleFilter(request) && | ||
!(new RegExp(util.escapeRegex(this.i18nData))).test(request)) { | ||
|
||
return true; | ||
} | ||
}); | ||
}; | ||
const dep = new CommonJsRequireDependency(this.i18nData, param.range); | ||
dep.loc = expr.loc; | ||
dep.optional = !!parser.scope.inTry; | ||
parser.state.current.addDependency(dep); | ||
|
||
// Hack to support webpack 1.x and 2.x. | ||
// webpack 2.x | ||
if (NormalModuleFactory.prototype.createParser) { | ||
compiler.plugin("compilation", function(compilation, params) { | ||
params.normalModuleFactory.plugin("parser", bindParser); | ||
return true; | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
// webpack 1.x | ||
} else { | ||
bindParser(compiler.parser); | ||
} | ||
}; | ||
} | ||
|
||
module.exports = DevelopmentModePlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,89 @@ | ||
var fs = require("fs"); | ||
var globalizeCompiler = require("globalize-compiler"); | ||
var path = require("path"); | ||
|
||
function GlobalizeCompilerHelper(attributes) { | ||
this.asts = {}; | ||
this.extracts = []; | ||
this.extractsMap = {}; | ||
this.modules = {}; | ||
|
||
this.cldr = attributes.cldr; | ||
this.developmentLocale = attributes.developmentLocale; | ||
this.messages = attributes.messages || {}; | ||
this.tmpdir = attributes.tmpdir; | ||
this.webpackCompiler = attributes.webpackCompiler; | ||
} | ||
"use strict"; | ||
|
||
const fs = require("fs"); | ||
const globalizeCompiler = require("globalize-compiler"); | ||
const path = require("path"); | ||
|
||
class GlobalizeCompilerHelper { | ||
constructor(attributes) { | ||
this.asts = {}; | ||
this.extracts = []; | ||
this.extractsMap = {}; | ||
this.modules = {}; | ||
|
||
this.cldr = attributes.cldr; | ||
this.developmentLocale = attributes.developmentLocale; | ||
this.messages = attributes.messages || {}; | ||
this.tmpdir = attributes.tmpdir; | ||
this.webpackCompiler = attributes.webpackCompiler; | ||
} | ||
|
||
GlobalizeCompilerHelper.prototype.setAst = function(request, ast) { | ||
this.asts[request] = ast; | ||
}; | ||
|
||
GlobalizeCompilerHelper.prototype.getExtract = function(request) { | ||
var ast, extract; | ||
if(!this.extractsMap[request]) { | ||
ast = this.asts[request]; | ||
extract = globalizeCompiler.extract(ast); | ||
this.extractsMap[request] = extract; | ||
this.extracts.push(extract); | ||
setAst(request, ast) { | ||
this.asts[request] = ast; | ||
} | ||
return this.extractsMap[request]; | ||
}; | ||
|
||
GlobalizeCompilerHelper.prototype.createCompiledDataModule = function(request) { | ||
var filepath = this.getModuleFilepath(request); | ||
this.modules[filepath] = true; | ||
getExtract(request) { | ||
let ast, extract; | ||
if(!this.extractsMap[request]) { | ||
ast = this.asts[request]; | ||
extract = globalizeCompiler.extract(ast); | ||
this.extractsMap[request] = extract; | ||
this.extracts.push(extract); | ||
} | ||
return this.extractsMap[request]; | ||
} | ||
|
||
fs.writeFileSync(filepath, this.compile(this.developmentLocale, request)); | ||
createCompiledDataModule(request) { | ||
const filepath = this.getModuleFilepath(request); | ||
this.modules[filepath] = true; | ||
|
||
return filepath; | ||
}; | ||
fs.writeFileSync(filepath, this.compile(this.developmentLocale, request)); | ||
|
||
GlobalizeCompilerHelper.prototype.getModuleFilepath = function(request) { | ||
// Always append .js to the file path to cater for non-JS files (e.g. .coffee). | ||
return path.join(this.tmpdir, request.replace(/.*!/, "").replace(/[\/\\?" :\.]/g, "-") + ".js"); | ||
}; | ||
return filepath; | ||
} | ||
|
||
GlobalizeCompilerHelper.prototype.compile = function(locale, request) { | ||
var content; | ||
var messages = this.messages; | ||
getModuleFilepath(request) { | ||
// Always append .js to the file path to cater for non-JS files (e.g. .coffee). | ||
return path.join(this.tmpdir, request.replace(/.*!/, "").replace(/[\/\\?" :\.]/g, "-") + ".js"); | ||
} | ||
|
||
var attributes = { | ||
cldr: this.cldr, | ||
defaultLocale: locale, | ||
extracts: request ? this.getExtract(request) : this.extracts | ||
}; | ||
compile(locale, request) { | ||
let content; | ||
|
||
if (messages[locale]) { | ||
attributes.messages = messages[locale]; | ||
} | ||
const attributes = { | ||
cldr: this.cldr, | ||
defaultLocale: locale, | ||
extracts: request ? this.getExtract(request) : this.extracts | ||
}; | ||
|
||
this.webpackCompiler.applyPlugins("globalize-before-compile-extracts", locale, attributes, request); | ||
|
||
try { | ||
content = globalizeCompiler.compileExtracts(attributes); | ||
} catch(e) { | ||
// The only case to throw is when it's missing formatters/parsers for the | ||
// whole chunk, i.e., when `request` isn't present; or when error is | ||
// something else obviously. If a particular file misses formatters/parsers, | ||
// it can be safely ignored (i.e., by using a stub content), because in the | ||
// end generating the content for the whole chunk will ultimately verify | ||
// whether or not formatters/parsers has been used. | ||
if (!/No formatters or parsers has been provided/.test(e.message) || !request) { | ||
throw e; | ||
if (this.messages[locale]) { | ||
attributes.messages = this.messages[locale]; | ||
} | ||
content = "module.exports = {};"; | ||
} | ||
|
||
// Inject set defaultLocale. | ||
return content.replace(/(return Globalize;)/, "Globalize.locale(\"" + locale + "\"); $1"); | ||
}; | ||
this.webpackCompiler.applyPlugins("globalize-before-compile-extracts", locale, attributes, request); | ||
|
||
try { | ||
content = globalizeCompiler.compileExtracts(attributes); | ||
} catch(e) { | ||
// The only case to throw is when it's missing formatters/parsers for the | ||
// whole chunk, i.e., when `request` isn't present; or when error is | ||
// something else obviously. If a particular file misses formatters/parsers, | ||
// it can be safely ignored (i.e., by using a stub content), because in the | ||
// end generating the content for the whole chunk will ultimately verify | ||
// whether or not formatters/parsers has been used. | ||
if (!/No formatters or parsers has been provided/.test(e.message) || !request) { | ||
throw e; | ||
} | ||
content = "module.exports = {};"; | ||
} | ||
|
||
GlobalizeCompilerHelper.prototype.isCompiledDataModule = function(request) { | ||
return request && this.modules[request.replace(/.*!/, "")]; | ||
}; | ||
// Inject set defaultLocale. | ||
return content.replace(/(return Globalize;)/, "Globalize.locale(\"" + locale + "\"); $1"); | ||
} | ||
|
||
isCompiledDataModule(request) { | ||
return request && this.modules[request.replace(/.*!/, "")]; | ||
} | ||
} | ||
|
||
module.exports = GlobalizeCompilerHelper; |
Oops, something went wrong.