Skip to content

Commit 0b0876a

Browse files
tricoder42Photonios
authored andcommitted
fix: set module type in webpack loader
Closes lingui#297 Related to: webpack/webpack#7057 (comment)
1 parent dd47253 commit 0b0876a

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎packages/loader/src/index.js‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
import path from "path"
2-
import R from "ramda"
2+
import * as R from "ramda"
33
import { getConfig } from "@lingui/conf"
44
import { createCompiledCatalog, configureCatalog } from "@lingui/cli/api"
55

6+
// Check if JavascriptParser and JavascriptGenerator exists -> Webpack 4
7+
let JavascriptParser
8+
let JavascriptGenerator
9+
try {
10+
JavascriptParser = require("webpack/lib/Parser")
11+
JavascriptGenerator = require("webpack/lib/JavascriptGenerator")
12+
} catch (error) {
13+
if (error.code !== "MODULE_NOT_FOUND") {
14+
throw e
15+
}
16+
}
17+
618
export default function(source) {
19+
// Webpack 4 uses json-loader automatically, which breaks this loader because it
20+
// doesn't return JSON, but JS module. This is a temporary workaround before
21+
// official API is added (https://github.com/webpack/webpack/issues/7057#issuecomment-381883220)
22+
// See https://github.com/webpack/webpack/issues/7057
23+
if (JavascriptParser && JavascriptGenerator) {
24+
this._module.type = "javascript/auto"
25+
this._module.parser = new JavascriptParser()
26+
this._module.generator = new JavascriptGenerator()
27+
}
28+
729
const config = getConfig({ cwd: path.dirname(this.resourcePath) })
830
const catalog = configureCatalog(config)
931

‎packages/loader/test/compiler.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export default (fixture, options = {}) => {
1414
rules: [
1515
{
1616
test: /\.json$/,
17-
type: "javascript/auto",
1817
use: {
1918
loader: path.resolve(__dirname, "../src/index.js")
2019
}

0 commit comments

Comments
 (0)