Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mina-loader: 为lang属性增加前置、主要、后置三个loader配置项 #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/mina-loader/lib/loaders/mina.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,22 @@ const getLoaders = (loaderContext, tag, options, attributes = {}) => {
return ''
}

// append custom loader
let custom = lang
? options.languages[lang] || `${lang}-loader`
const getCustomizedLoader = (lang, { loaders = {}, languages = {} }) => {
const beforeLoader = languages[`${lang}:before`]
const mainLoader = languages[`${lang}:main`] || loaders[tag]
const afterLoader = languages[`${lang}:after`] || languages[lang]
const toArray = val => {
if (val === undefined || val === null || val === '') {
return []
}
return Array.isArray(val) ? val : [val]
}
return [...toArray(beforeLoader), ...toArray(mainLoader), ...toArray(afterLoader)]
}

// HACK: 一旦getLoaders函数内发生异常,测试代码没有显示任何异常,应该是在某个地方压制住了它。望改善!
let custom = lang
? (Object.keys(options.languages).length === 0 ? `${lang}-loader` : getCustomizedLoader(lang, options))
: options.loaders[tag] || ''
if (custom) {
custom = helpers.stringifyLoaders(
Expand Down
74 changes: 74 additions & 0 deletions packages/mina-loader/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/mina-loader/test/fixtures/lang/override-loader.mina
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="yellowify">
<script lang="number">
Page({
onLoad () {
console.log('blue');
console.log(9);
}
})
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* 找出其中的所有数字,每个加上1.
*/

module.exports = function(source) {
return source.replace(/\d+/g, val => Number(val) + 1)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* 找出其中的所有数字,每个乘以2.
*/

module.exports = function(source) {
return source.replace(/\d+/g, val => Number(val) * 2)
}
85 changes: 74 additions & 11 deletions packages/mina-loader/test/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ test('use lang attribute with extra rules', async t => {
}
})

test('use lang attribute should override loaders options', async t => {
test('use lang before attribute should prepend loaders options', async t => {
try {
const { compile, mfs } = compiler({
entry: './fixtures/lang/override-loader.mina',
Expand All @@ -83,42 +83,105 @@ test('use lang attribute should override loaders options', async t => {
loader: require.resolve('..'),
options: {
loaders: {
script: 'babel-loader',
script: './helpers/loaders/number-multiply-two-loader.js',
},
languages: {
yellowify: './helpers/loaders/replace-blue-to-yellow',
'number:before': './helpers/loaders/number-add-one-loader.js', // (9*2)+1
},
},
},
},
],
},
})
const stats = await compile()
await compile()

t.true(
mfs
.readFileSync('/fixtures/lang/override-loader.js', 'utf8')
.includes("console.log('yellow')")
.includes("console.log(19)")
)
t.false(
} catch (error) {
console.error(error)
}
})

test('use lang after attribute should append loaders options', async t => {
try {
const { compile, mfs } = compiler({
entry: './fixtures/lang/override-loader.mina',
output: {
filename: 'fixtures/lang/override-loader.js',
},
module: {
rules: [
{
test: /\.mina$/,
use: {
loader: require.resolve('..'),
options: {
loaders: {
script: './helpers/loaders/number-multiply-two-loader.js',
},
languages: {
'number:after': './helpers/loaders/number-add-one-loader.js', // (9+1)*2
},
},
},
},
],
},
})
await compile()

t.true(
mfs
.readFileSync('/fixtures/lang/override-loader.js', 'utf8')
.includes("console.log('blue')")
.includes("console.log(20)")
)
} catch (error) {
console.error(error)
}
})

test('use lang main attribute should override loaders options', async t => {
try {
const { compile, mfs } = compiler({
entry: './fixtures/lang/override-loader.mina',
output: {
filename: 'fixtures/lang/override-loader.js',
},
module: {
rules: [
{
test: /\.mina$/,
use: {
loader: require.resolve('..'),
options: {
loaders: {
script: './helpers/loaders/number-multiply-two-loader.js',
},
languages: {
'number:main': './helpers/loaders/number-add-one-loader.js', // 9+1
},
},
},
},
],
},
})
await compile()

t.true(
mfs
.readFileSync('/fixtures/lang/override-loader.js', 'utf8')
.includes('onLoad () {')
.includes("console.log(10)")
)
t.false(
mfs
.readFileSync('/fixtures/lang/override-loader.js', 'utf8')
.includes('onLoad: function onLoad() {')
.includes("console.log(18)")
)

t.pass()
} catch (error) {
console.error(error)
}
Expand Down